package com.lenovo.locationmanager;

import android.content.Intent;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.util.Log;


import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: GpsAutoTest
 * @Package: com.lenovo.gpsautotest
 * @ClassName: GpsStatusListener
 * @Description: GpsStatusListener类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/5/30 15:08
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/5/30 15:08
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class GpsStatusListener implements GpsStatus.Listener {
    private static final String TAG = GpsStatusListener.class.getSimpleName();
    private GpsService service;
    private SatelliteResult satelliteResult;
    private com.lenovo.locationmanager.GpsSatellite gpsSatellite;

    private List<com.lenovo.locationmanager.GpsSatellite> gpsSatelliteList = new ArrayList<>();
    private GpsStatus gpsStatus;

    public GpsStatusListener(GpsService service) {
        this.service = service;
        satelliteResult = new SatelliteResult();
    }

    @Override
    public void onGpsStatusChanged(int event) {
        gpsStatus = service.locationManager.getGpsStatus(null);

        switch (event) {
            //第一次定位
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                Log.i(TAG, "第一次定位");
                Intent ttffIntent = new Intent(GpsIntent.TIME_TO_FIRST_FIX);
                ttffIntent.putExtra(GpsIntent.TTFF, gpsStatus.getTimeToFirstFix());
                ttffIntent.putExtra(GpsIntent.CURRENT_SESSION, service.mCurrentSessionNo);
                service.sendBroadcast(ttffIntent);
                return;
            //卫星状态改变
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                Log.i(TAG, "卫星状态改变");
                Intent satelliteIntent = new Intent(GpsIntent.GPS_SATELLITE_INFO);
                //获取卫星颗数的默认最大值
                satelliteResult.setMaxSatellite(gpsStatus.getMaxSatellites());
                //创建一个迭代器保存所有卫星
                gpsSatelliteList.clear();
                Log.d(TAG, "onGpsStatusChanged: ------------------------");
                for (GpsSatellite satellite : gpsStatus.getSatellites()) {
                    if (satellite != null) {
                        Log.d(TAG, "onGpsStatusChanged: "+satellite.getSnr());
                        gpsSatellite=new com.lenovo.locationmanager.GpsSatellite();
                        gpsSatellite.setmPrn(satellite.getPrn());
                        gpsSatellite.setmSnr(satellite.getSnr());
                        gpsSatellite.setmElevation(satellite.getElevation());
                        gpsSatellite.setmAzimuth(satellite.getAzimuth());
                        gpsSatellite.setmHasAlmanac(satellite.hasAlmanac());
                        gpsSatellite.setmHasEphemeris(satellite.hasEphemeris());
                        gpsSatellite.setmUsedInFix(satellite.usedInFix());
                        gpsSatelliteList.add(gpsSatellite);
                    }
                }
                satelliteResult.setGpsSatellites(gpsSatelliteList);
                Log.d(TAG, "onGpsStatusChanged: "+satelliteResult.toString());
                satelliteIntent.putExtra(GpsIntent.SATELLITES, satelliteResult);
                service.sendBroadcast(satelliteIntent);
                return;
            //定位启动
            case GpsStatus.GPS_EVENT_STARTED:
                Log.i(TAG, "定位启动");
                break;
            //定位结束
            case GpsStatus.GPS_EVENT_STOPPED:
                Log.i(TAG, "定位结束");
                break;
        }
    }
}
